@extends('admin.main')
@section('head')
    @include('UEditor::head')
    <style>
        .upload-button {
            position: relative;
            display: inline-block;
            padding: 4px 12px;
            overflow: hidden;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
            color: #fff;
            background-color: #27ae60;
            text-align: center;
            width: 100px;
        }
        .upload-button input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }
        .upload-img-box .upload-pre-item {
            float: left;
            padding: 0px;
            margin: 1px 10px 1px 1px;
            width: 150px;
            max-height: 180px;
            overflow: hidden;
            text-align: center;
            cursor: pointer;
            border: 1px solid #ccc;
            transition: all .3s linear;
        }
        .upload-pre-item img{
            border-bottom: 1px solid #ccc;
        }
        .upload-pre-item div{
            background-color: lightgrey;
        }

    </style>
@endsection
@section('body')
<div class="main-title">
    <h2>{{isset($info['id']) ? '编辑':'新增'}}商品</h2>
</div>
<div class="tab-wrap">
    <div class="tab-content">
        <form action="" method="post" class="form-horizontal">
            <!-- 基础 -->
            <div id="tab1" class="tab-pane in tab1">
                <div class="form-item">
                    <label class="item-label">
                        商品名称<span class="check-tips">（名称不能为空）</span>
                    </label>
                    <div class="controls">
                        <input type="text" name="goods_name" class="text input-large" placeholder="例如:娃哈哈纯净水18.9L" value="{{isset($info['goods_name']) ? $info['goods_name'] : ''}}">
                    </div>
                </div>
                <div class="controls">
                    <label class="item-label">商品图片<span class="check-tips">（请上传尺寸为400*400的图片，格式为jpg、png、gif，上传图片大小必须在2M以下）</span></label>
                    @if(!isset($info))
                    <div class="upload-button">
                        <span class="uploadify-button-text">上传图片</span>
                        <input type="file" id="uploadImage" accept="image/png,image/jpeg,image/gif" num="5"
                               onchange="upload(this,'goods_img','pics[]')" count="{{isset($info) ? $info['pic_num'] : 0}}"/>
                    </div>
                    @endif
                    <input type="hidden" name="goods_img" id="goods_img" value="{{isset($info['goods_img']) ? $info['goods_img'] : ''}}"/>
                    {{--<input type="hidden" name="img_id" id="img_id" value="{{isset($info['img_id']) ? $info['img_id'] : ''}}"/>--}}
                    <div class="upload-img-box">

                        @if(isset($info) && $info['pic_num'])
                            @for ($i = 1; $i <= $info['pic_num']; $i++)
                                @if(file_exists(public_path().'/uploads/goods/imgs/'.$info['admin_id'].'/'.$info['goods_id'].'_'.$i.'.'.$ext))
                            <div class="upload-pre-item"  id="goods_img_{{$i}}">
                                <img src="/uploads/goods/imgs/{{$info['admin_id']}}/{{$info['goods_id']}}_{{$i}}.{{$ext}}?{{time()}}"/>
                                {{--<div class="goods_img_{{$i}}" onclick="delImg('uploadImage','goods_img_{{$i}}')">删除</div>--}}
                                <input type="hidden" name="pics[]" class="goods_img_{{$i}}"  value="/uploads/goods/imgs/{{$info['admin_id']}}/{{$info['goods_id']}}_{{$i}}.{{$ext}}">
                            </div>
                                @endif
                            @endfor
                        @endif
                    </div>
                </div>
                <div class="form-item">
                    <label class="item-label">商品分类<span class="check-tips"></span></label>
                    <div class="controls">
                        <select name="category_id" readonly>
                            @if(count($goodsCategory))
                                @foreach($goodsCategory as $v)
                                    <option value="{{$v['id']}}"
                                            @if(isset($info['category_id']) && $info['category_id'] == $v['id']) selected @endif>{{$v['prefix']}}{{$v['title']}}</option>
                                @endforeach
                            @endif
                        </select>
                    </div>
                </div>
                <div class="form-item">
                    <label class="item-label">商品品牌<span class="check-tips"></span></label>
                    <div class="controls">
                        <select name="brand_id" readonly>
                            @if(count($brand))
                                @foreach($brand as $v)
                                    <option value="{{$v['id']}}"
                                            @if(isset($info['brand_id']) && $info['brand_id'] == $v['id']) selected @endif>{{$v['title']}}</option>
                                @endforeach
                            @endif
                        </select>
                    </div>
                </div>
                <div class="form-item">
                    <label class="item-label">水桶品牌<span class="check-tips"></span></label>
                    <div class="controls">
                        <select name="bucket_category_id" readonly>
                            @if(count($bucketCategory))
                                @foreach($bucketCategory as $v)
                                    <option value="{{$v['id']}}"
                                            @if(isset($info['bucket_category_id']) && $info['bucket_category_id'] == $v['id']) selected @endif>{{$v['title']}}</option>
                                @endforeach
                            @endif
                        </select>
                    </div>
                </div>
                <div class="form-item">
                    <label class="item-label">
                        市场价格<span class="check-tips">（价格不能小于0）</span>
                    </label>
                    <div class="controls">
                        <input type="text" name="shop_price" class="text input-large" value="{{isset($info['shop_price']) ? $info['shop_price'] : 0}}">
                    </div>
                </div>
                <div class="form-item">
                    <label class="item-label">
                        商品价格<span class="check-tips">（价格不能小于0）</span>
                    </label>
                    <div class="controls">
                        <input type="text" name="goods_price" class="text input-large" value="{{isset($info['goods_price']) ? $info['goods_price'] : 0}}">
                    </div>
                </div>
                <div class="form-item">
                    <label class="item-label">
                        商品库存<span class="check-tips">（价格不能小于0）</span>
                    </label>
                    <div class="controls">
                        <input type="text" name="goods_num" class="text input-large" value="{{isset($info['goods_num']) ? $info['goods_num'] : 0}}">
                    </div>
                </div>

                {{--<div class="form-item">
                    <label class="item-label">商品详情</label>
                    <div class="controls">
                            <script id="container" name="goods_content" type="text/plain">{!! isset($info['goods_content']) ? $info['goods_content'] : '' !!}</script>
                        <!-- 实例化编辑器 -->
                        <script type="text/javascript">
                            var ue = UE.getEditor('container');
                        </script>
                    </div>
                </div>--}}
                <script type="text/javascript">
                    //上传图片
                    /* 初始化上传插件 */

                </script>
            </div>
            <div class="form-item">
                <label class="item-label">排序<span class="check-tips">（用于前台显示的顺序，越小越靠前）</span></label>
                <div class="controls">
                    <input type="text" class="text input-small" name="sort" value="{{isset($info['sort']) ? $info['sort'] : 0}}">
                </div>
            </div>
            <!-- 高级 -->
            <div class="form-item">
                <input type="hidden" name="id" value="{{isset($info['id']) ? $info['id'] : ''}}">
                <input type="hidden" name="admin_id" value="{{isset($info['admin_id']) ? $info['admin_id'] : ''}}">
                <input type="hidden" name="shop_id" value="{{isset($info['shop_id']) ? $info['shop_id'] : ''}}">
                <input type="hidden" name="goods_id" value="{{isset($info['goods_id']) ? $info['goods_id'] : ''}}">
                <input type="hidden" name="status" value="{{isset($info['status']) ? $info['status'] : 0}}">
                <button type="submit" id="submit" class="btn submit-btn ajax-post" target-form="form-horizontal">确 定
                </button>
                <button class="btn btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>
            </div>
        </form>
    </div>
</div>
@endsection

@section('script')
    <script type="text/javascript">

        function delImg(upInputId, fileId) {
            var _this = $("#" + upInputId);
            var count = parseInt(_this.attr('count'));
            _this.attr('count', count - 1);
            $("#" + fileId).remove();
        }
        function upload(obj, imgId, name) {
            var _self = obj;
            var _this = $(obj);
            var fileReader = new FileReader();
            fileReader.onload = function (e) {
                if (fileReader.readyState == FileReader.DONE) {
                    var file = _self.files[0];
                    var type = file.type;
                    var size = parseInt(file.size);
                    var allowSize = parseInt(2097152); //限制上传图片大小1M以下,2*1024*1024
                    if (size > allowSize) {
                        layer.msg('上传图片大小必须在2M以下');
                        return false;
                    }
                    var accept = _this.attr('accept');//上传图片数量
                    var param_num = _this.attr('num');//上传图片数量
                    var param_count = _this.attr('count');//已上传图片数量
                    var acceptArr = accept.split(",");
                    if ($.inArray(type, acceptArr) < 0) {
                        layer.msg('请上传'+accept+'等格式的文件');
                        return false;
                    }
                    if (param_num == null || param_num == undefined) {
                        _this.attr('num', 1);
                    }
                    if (param_count == null || param_count == undefined) {
                        _this.attr('count', 0);
                    }
                    var num = parseInt(_this.attr('num'));
                    var count = parseInt(_this.attr('count'));
                    if (count < num) {
                        count = count + 1;
                        var upFileId = _this.attr('id');
                        var fileId = imgId + '_' + count;
                        var func = 'delImg("' + upFileId + '","' + fileId + '")';
                        $("#" + imgId).parent().find('.upload-img-box').append(
                                '<div class="upload-pre-item" id="' + fileId + '"><img src="' + this.result + '"/><div class="' + fileId + '" onclick=' + func + '>删除</div><input type="hidden" name="' + name + '" class="' + fileId + '"  value="' + this.result + '"></div>'
                        );
                        _this.attr('count', count);
                    } else {
                        layer.msg('最多上传' + num + '张');
                        return false;
                    }
                    _self.value = ''; //清空上传成功后的value值
                }
            };
            //一次加载多个文件
            var i = 0, src = _self.files;
            fileReader.readAsDataURL(src[i]);
            fileReader.onloadend = function () {
                i++;
                if (i < src.length) fileReader.readAsDataURL(src[i]);
            }
        }
    </script>
@endsection
